﻿using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Tools;

namespace E_F_H_GCoordinates
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string Text;
            //判断文本框格式
            if (textBox4.Text != "" && textBox5.Text != "" && textBox6.Text != "" && textBox7.Text != "")
            {
                //获取文本框的值
                string[] A = textBox1.Text.Split(',', '，');
                string[] O = textBox2.Text.Split(',', '，');
                string[] B = textBox3.Text.Split(',', '，');
                double OYNorm = double.Parse(textBox4.Text.ToString());
                double CDNorm = double.Parse(textBox5.Text.ToString());
                double FYNorm = double.Parse(textBox6.Text.ToString());
                double GYNorm = double.Parse(textBox7.Text.ToString());
                //判断坐标格式是否正确
                if (A.Length != 2 || O.Length != 2 || B.Length != 2)
                {
                    MessageBox.Show("坐标格式错误，请重新输入坐标！！");
                    return;
                }
                ArrayList list = new ArrayList();
                for (int i = 0; i < A.Length; i++)
                {
                    list.Add(A[i]);
                    list.Add(B[i]);
                    list.Add(O[i]);
                }
                foreach (string item in list)
                {
                    if (RegularExpression(item) != true)
                    {
                        MessageBox.Show("坐标格式错误，请重新输入坐标！！");
                        return;
                    }
                }
                //AB向量
                Point pointA = new Point(double.Parse(A[0]), double.Parse(A[1]));
                Point pointO = new Point(double.Parse(O[0]), double.Parse(O[1]));
                Point pointB = new Point(double.Parse(B[0]), double.Parse(B[1]));
                Vector vectorOA = new Vector(pointA.X - pointO.X, pointA.Y - pointO.Y);
                Vector vectorOB = new Vector(pointB.X - pointO.X, pointB.Y - pointO.Y);
                //求出AOB角度
                double Angle = System.Math.Acos((vectorOA.X * vectorOB.X + vectorOA.Y * vectorOB.Y) / (Vector.Norm(vectorOA.X, vectorOA.Y) * Vector.Norm(vectorOB.X, vectorOB.Y))) * 180 / System.Math.PI;
                //利用旋转角计算Y,F点
                Point pointY = Tools.Math.Rotation(pointO, pointB, OYNorm, 0.5 * Angle);
                Point pointF = Tools.Math.Rotation(pointY, pointO, FYNorm, -90);
                //利用四点求交点 计算 FY和OA的交点点I坐标 
                Point pointI = Tools.Math.VectorIntersection(pointO, pointA, pointY, pointF);
                //利用相似三角形求出边长【IF/IY】比例计算点E到点F的长度
                Vector vectorIF = new Vector(pointI.X - pointF.X, pointI.Y - pointF.Y);
                Vector vectorIY = new Vector(pointI.X - pointY.X, pointI.Y - pointY.Y);
                double IFNorm = Vector.Norm(vectorIF.X, vectorIF.Y);
                double IYNorm = Vector.Norm(vectorIY.X, vectorIY.Y);
                //EF=OY*IF/IY
                double EFNorm = OYNorm * IFNorm / IYNorm;
                //旋转角得到点E
                Point pointE = Tools.Math.Rotation(pointF, pointY, EFNorm, -90);
                //同理计算G,H点
                //利用旋转角计算G点
                Point pointG = Tools.Math.Rotation(pointY, pointO, GYNorm, 90);
                //GY和OB的交点坐标
                Point pointJ = Tools.Math.VectorIntersection(pointO, pointB, pointY, pointG);
                //利用相似三角形求出边长【JF/JY】比例计算点G到点H的长度
                Vector vectorJG = new Vector(pointJ.X - pointG.X, pointJ.Y - pointG.Y);
                Vector vectorJY = new Vector(pointJ.X - pointY.X, pointJ.Y - pointY.Y);
                double JGNorm = Vector.Norm(vectorJG.X, vectorJG.Y);
                double JYNorm = Vector.Norm(vectorJY.X, vectorJY.Y);
                //HG=OY*JG/JY
                double HGNorm = OYNorm * JGNorm / JYNorm;
                //旋转角得到点H
                Point pointH = Tools.Math.Rotation(pointG, pointY, HGNorm, 90);
                Text = "E:<" + pointE.X + "," + pointE.Y + ">\r\nF:<" + pointF.X + "," + pointF.Y + ">,\r\n" +
                    "H:<" + pointH.X + "," + pointH.Y + ">\r\nG:<" + pointG.X + "," + pointG.Y + ">\r\n";
                textBox8.Text = Text;
            }
            else
            {
                MessageBox.Show("数值不可以为空!错误");
                return;
            }
        }
        //判断文本框出入的格式是否正确；
        bool RegularExpression(string y)
        {
            bool Expression = Regex.IsMatch(y, @"(^-?[0-9]{1,}$)|(^-?[0-9]{1,}\.{1}[0-9]{1,}$)");
            return Expression;
        }
        //限制文本框只能输入数字
        private void LimitNumbers(KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 45)
            {
                e.Handled = true;
                MessageBox.Show("请输入有效数值！！");
            }
        }
        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            LimitNumbers(e);
        }

        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {
            LimitNumbers(e);
        }

        private void textBox6_KeyPress(object sender, KeyPressEventArgs e)
        {
            LimitNumbers(e);
        }

        private void textBox7_KeyPress(object sender, KeyPressEventArgs e)
        {
            LimitNumbers(e);
        }
    }
}
